package com.atguigu.principle.singleresponsibility.singleton.type4;

/**
 * @Author: yangtao
 * @Description:
 */
public class Singleton5 {

    public static void main(String[] args) {
        System.out.println("双重检查方式");
      Singleton instance = Singleton.getInstance();
       Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
    }
}

//懒汉式(线程安全,同步方式)
class Singleton{
   private static volatile Singleton instance;

   private Singleton(){

   }

   //提供一个静态公有方法 使用到方法 采取创建，同步代码块
    //同时解决懒加载的问题 也保证了效率
    public static synchronized Singleton getInstance(){
       if(instance == null){
           synchronized (Singleton.class){
               if(instance == null){
                   instance = new Singleton();
               }
           }
       }
       return instance;
    }


}
